<?php

namespace Home\Model;

use Think\Model;

class KeywordModel extends Model {
	protected $trueTableName = '';
	public function getKeyWork($key) {

		// 读取缓存
		$cacheName = get_cache_name ( 'keyword_' . $key );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}

		$sql = "SELECT `name` FROM " . C ( 'DB_PREFIX' ) . "site WHERE name like '{$key}%' or url like '{$key}%' group by name order by sort desc,viewnum desc limit 7";
		$rs = $this->query ( $sql );
		if (! count($rs)) {
			$sql = "SELECT `name` FROM " . C ( 'DB_PREFIX' ) . "tag WHERE name like '{$key}%' group by name order by id desc limit 7";
			$rs = $this->query ( $sql );
		}
		foreach ( $rs as $v ) {
			$result ['data'] [] ['title'] = $v ['name'];
		}
		if (count ( $rs )) {
			S ( $cacheName, $result, C ( 'cache_time_long' ) );
		}
		return $result;
	}
}